/**
 * 
 */
package vn.smartware.co.framework.validator;

import java.io.Serializable;
import java.util.ArrayList;

import javax.servlet.ServletContext;

import vn.smartware.co.framework.config.ActionMappingsConfig;
import vn.smartware.co.framework.config.SystemConfig;
import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.exception.FatalException;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.form.ActionForm;


/**
 * ActionMessage
 */
@SuppressWarnings("rawtypes")
public class ActionMessage implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 2323431004018104575L;

	/**
	 * <p>The message key for this message.</p>
	 */
	protected String key = null;
	
	/**
	 * <p>The replacement values for this message.</p>
	 */
	protected Object values[] = null;
	
	/**
	 * <p>The message for User.</p>
	 */
	protected String message = null;
		
	/**
	 * <p>Default Construct</p>
	 *
	 */
	public ActionMessage() {
	}
	
	/**
	 * <p>Construct an action message with the specified replacement values.</p>
	 * 
	 * @param key
	 * @param values
	 */
	public ActionMessage(String key, Object[] values) {
		
		this.key = key;
		this.values = values;
	}
	
	/**
	 * @return key
	 * 
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @return values
	 * 
	 */
	public Object[] getValues() {
		return values;
	}

	
	/**
	 * <p>Returns a String in the format: key[value1, value2, etc].</p>
	 * @return String
	 */
	public String toString() {
		StringBuffer buff = new StringBuffer();
		buff.append(this.key);
		buff.append("[");
		
		if (this.values != null) {
			
			for (int i=0; i < this.values.length; i++) {
				buff.append(this.values[i]);
				
				// don't append comma to last entry
				if (i < this.values.length -1) {
					buff.append(", ");
				}
			}
		}
		
		buff.append("]");
		
		return buff.toString();
	}

	/**
	 * @return message
	 * 
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message message
	 * 
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * getObjToMsgIsSet
	 * @param config
	 * @param form
	 * @param key
	 * @return ActionMessage
	 * @throws Exception 
	 */
	public static ActionMessage getObjToMsgIsSet(SystemConfig config, ActionForm form, String key)
	                                       throws Exception {
		
		ActionMappingsConfig actionConfig = config.getActionMappingConfig(form.getActionForm());
		
		if (actionConfig == null) {
			throw new ServerException("ActionMappingConfig is Not Found.");
		}
		
		ArrayList list = actionConfig.getArgslist();
		Object[] argsObj = list.toArray();
		
		ActionMessage messageObj = ActionMessage.getObjToMsgIsSet(key, argsObj);
		
		return messageObj;
		
	}

	/**
	 * getObjToMsgIsSet
	 * @param config
	 * @param form
	 * @param key
	 * @param context ServletContext
	 * @return ActionMessage
	 * @throws Exception
	 */
	public static ActionMessage getObjToMsgIsSet(SystemConfig config, ActionForm form, String key, ServletContext context)
	                                       throws Exception {
		
		ActionMappingsConfig actionConfig = config.getActionMappingConfig(form.getActionForm());
		
		if (actionConfig == null) {
			throw new ServerException("ActionMappingConfig is Not Found.");
		}
		
		ArrayList list = actionConfig.getArgslist();
		Object[] argsObj = list.toArray();
		
		ActionMessage messageObj = ActionMessage.getObjToMsgIsSet(key, argsObj, context);
		
		return messageObj;
		
	}
	
	/**
	 * getObjToMsgIsSet
	 * @param key
	 * @param values
	 * @return ActionMessage
	 * @throws ClientException
	 */
	public static ActionMessage getObjToMsgIsSet(String key, Object[] values)
	                                       throws ClientException {
		
		ActionMessage messageObj = new ActionMessage(key, values);
		
		String message = Resources.getMessage(messageObj);
		
		if (message != null) {
			messageObj.setMessage(message);
		} else {
			throw new ClientException("Message Resources value Not Found. key=" + key);
		}
		
		return messageObj;
	}

	/**
	 * getObjToMsgIsSet
	 * @param key
	 * @param values
	 * @param context ServletContext
	 * @return ActionMessage
	 * @throws FatalException
	 */
	public static ActionMessage getObjToMsgIsSet(String key, Object[] values, ServletContext context)
	                                       throws FatalException {
		
		ActionMessage messageObj = new ActionMessage(key, values);
		
		String message = Resources.getMessage(messageObj, context);
		
		if (message != null) {
			messageObj.setMessage(message);
		} else {
			throw new ServerException("Message Resources value Not Found. key=" + key);
		}
		
		return messageObj;
	}	
}
